<template>
  <el-container class="login-wrap">
    <el-header>
      <h1 class="title">Money 证券交易系统</h1>
    </el-header>
    <el-main>
      <el-row :gutter="20">
        <el-col :span="4" :offset="10">
          <div class="login-container">
            <el-form :model="loginForm" :rules="rules" ref="loginForm">
              <el-form-item prop="uid">
                <el-input type="text" placeholder="用户名" v-model="loginForm.uid"/>
              </el-form-item>
              <el-form-item prop="password">
                <el-input type="password" placeholder="密码" v-model="loginForm.password"/>
              </el-form-item>
              <el-row>
                <el-col :span="12">
                  <el-form-item prop="captcha">
                    <el-input type="text"
                              placeholder="验证码"
                              autocomplete="off"
                              v-model="loginForm.captcha"
                              @keyup.enter.native="submitForm('loginForm')"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <img :src="codeImage" @click="refreshCode">
                </el-col>
              </el-row>
              <el-form-item>
                <el-button :loading="loading" type="primary" style="width: 100%" @click="submitForm('loginForm')">登录</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
      </el-row>
    </el-main>
    <el-footer>
      <h6> 发财科技金融有限 版权所有@2023</h6>
    </el-footer>
  </el-container>
</template>

<script>

import {queryCaptcha, login} from "../api/loginApi";
import encryptMD5 from "js-md5";

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        uid: '',
        password: '',
        captcha: '',
        captchaId: ''
      },
      codeImage: '',
      loading: false,
      rules: {
        uid: [{required: true, message: '请输入账号', trigger: 'blur'}],
        password: [{required: true, message: '请输入密码', trigger: 'blur'}],
        captcha: [{required: true, message: '请输入验证码', trigger: 'blur'}]
      }
    }
  },
  created() {
    this.refreshCode()
  },
  methods: {
    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.loading = true
          login({
            uid: this.loginForm.uid,
            password: encryptMD5(this.loginForm.password),
            captcha: this.loginForm.captcha,
            captchaId: this.loginForm.captchaId
          }, this.loginCallback)
        } else {
          this.loading = false
        }
      })
    },
    loginCallback(code, msg, data) {
      if (code == 500) { // 不需要
        this.$message.error(msg)
        this.loading = false
        this.refreshCode()
      }else {
        sessionStorage.setItem('uid', data.uid)
        sessionStorage.setItem('token', data.token)
        if ( data.lastLoginDate.length > 1) {
          this.$message.success("登录成功,上次登录时间为:" + data.lastLoginDate + " " + data.lastLoginTime)
        }else {
          this.$message.success("登录成功~")
        }
        // 跳转主页面
        setTimeout(() => {
          this.loading = false
          this.$router.push({path: '/dashboard'})
        }, 1000) // 1秒延时
      }
    },
    // 刷新验证码
    refreshCode() {
      queryCaptcha(this.captchaCallback)
    },
    captchaCallback(code, msg, data) {
      if (code == 500) {
        this.$message.error(msg)
      }else {
        this.loginForm.captchaId = data.uuid
        this.codeImage= data.imageBase64
      }
    }

  }
}
</script>

<style scoped>

.title {
  color: #ebb563;
  font-size: 80px;
}

.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  background-image: url("../assets/img/login-bg.jpg");
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100% 100%;
  background-attachment: fixed;
  padding-top: 3%;
}

.login-container {
  background: #2c3e50;
  border-radius: 10px;
  padding: 60px 20px;
  margin-top: 120px;
  box-shadow: 0px 0px 20px 20px rgb(0, 0, 0, 0.1);
}
</style>
